package com.zrkizzy.seckill.config;

import com.zrkizzy.seckill.config.mapper.JacksonObjectMapper;
import com.zrkizzy.seckill.config.resolver.UserArgumentResolver;
import com.zrkizzy.seckill.interceptor.AccessLimitInterceptor;
import com.zrkizzy.seckill.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * WebMvc配置类
 *
 * @author zhangrongkang
 * @since 2023/3/21
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private AccessLimitInterceptor accessLimitInterceptor;

    @Autowired
    private UserArgumentResolver userArgumentResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截跳转登录页面请求
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(
                "/js/**",
                "/css/**",
                "/image/**",
                "/bootstrap/**",
                "/layer/**",
                "/jquery-validation/**",
                "/login/**",
                "/produce/**",
                "/");
        // 自定义限流拦截器
        registry.addInterceptor(accessLimitInterceptor);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将"/js/**", "/bootstrap/**", "/layer/**"请求中的静态资源放行
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/image/**").addResourceLocations("classpath:/static/image/");
        registry.addResourceHandler("/bootstrap/**").addResourceLocations("classpath:/static/bootstrap/");
        registry.addResourceHandler("/layer/**").addResourceLocations("classpath:/static/layer/");
        registry.addResourceHandler("/jquery-validation/**").addResourceLocations("classpath:/static/jquery-validation/");
        // 跳转订单详情页面
        registry.addResourceHandler("/produce/**").addResourceLocations("classpath:/static/web/");
    }

    /**
     * 重写MVC框架消息转换器
     *
     * @param converters 消息转换器对象
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        // 设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        // 将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0, messageConverter);
    }
}
